/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_hdi_test_entry.h"
#include "sle_adapter_hdi.h"
#include "sle_announce_hdi.h"
#include "sle_unit_test_log.h"

SleAnnounceHdiCallbacks *g_announceHdiUtCb;
SleAnnounceHdiOpts *g_announceHdiUtOpts = NULL;

void OnSleHdiUtStartAnnounceCallback(uint32_t announceId, SleHdiErrorCode errCode)
{
    SLE_UT_LOG("[SleAnnounceHdiTest][PASSED]OnSleHdiUtStartAnnounceCallback");
}

void OnSleHdiUtStopAnnounceCallback(uint32_t announceId, SleHdiErrorCode errCode)
{
    SLE_UT_LOG("[SleAnnounceHdiTest][PASSED]OnSleHdiUtStartAnnounceCallback");
}

void OnSleHdiUtTerminalAnnounceCallback(uint32_t announceId, SleHdiErrorCode errCode)
{
    SLE_UT_LOG("[SleAnnounceHdiTest][PASSED]OnSleHdiUtStartAnnounceCallback");
}

SleHdiErrorCode InitSleAnnounceHdiTestEntry(void)
{
    g_announceHdiUtOpts = (SleAnnounceHdiOpts *)malloc(sizeof(SleAnnounceHdiOpts));
    SLE_UT_CHECK_AND_RETURN_RET_LOG(g_announceHdiUtOpts != NULL, SLE_HDI_ERRCODE_FAIL, "[SleAnnounceHdiTest][FAILED]InitSleAnnounceHdiTestEntry malloc failed");
    g_announceHdiUtCb = (SleAnnounceHdiCallbacks *)malloc(sizeof(SleAnnounceHdiCallbacks));
    SLE_UT_CHECK_AND_RETURN_RET_LOG(g_announceHdiUtCb != NULL, SLE_HDI_ERRCODE_FAIL, "[SleAnnounceHdiTest][FAILED]InitSleAnnounceHdiTestEntry malloc failed");
    g_announceHdiUtCb->OnAnnounceStartCb = OnSleHdiUtStartAnnounceCallback;
    g_announceHdiUtCb->OnAnnounceStopCb = OnSleHdiUtStopAnnounceCallback;
    g_announceHdiUtCb->OnAnnounceTerminalCb = OnSleHdiUtTerminalAnnounceCallback;
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode DeinitSleAnnounceHdiTestEntry(void)
{
    free(g_announceHdiUtCb);
    free(g_announceHdiUtOpts);
    return SLE_HDI_ERRCODE_SUCC;
}

void GetSleAnnounceHdiOptsTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = GetSleAnnounceHdiOpts(g_announceHdiUtOpts);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleAnnounceHdiTest][FAILED] GetSleAnnounceHdiOptsTestLevel0 failed(%08X)",ret);
    SLE_UT_LOG("[SleAnnounceHdiTest][PASSED]GetSleAnnounceHdiOptsTestLevel0");
}

static uint8_t g_announceId = 1;
void SleHdiStartAnnounceTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = g_announceHdiUtOpts->startAnnounce(g_announceId);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleAnnounceHdiTest][FAILED] SleHdiStartAnnounceTestLevel0 failed(%08X)",ret);
    SLE_UT_LOG("[SleAnnounceHdiTest][PASSED]SleHdiStartAnnounceTestLevel0");
}

void SleHdiRegisterAnnounceCallbacksTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = g_announceHdiUtOpts->registerAnnounceCbks(g_announceHdiUtCb);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleAnnounceHdiTest][FAILED] SleHdiRegisterAnnounceCallbacksTestLevel0 failed(%08X)",ret);
    SLE_UT_LOG("[SleAnnounceHdiTest][PASSED]SleHdiRegisterAnnounceCallbacksTestLevel0");
}

void SleHdiStopAnnounceTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = g_announceHdiUtOpts->stopAnnounce(g_announceId);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleAnnounceHdiTest][FAILED] SleHdiStopAnnounceTestLevel0 failed(%08X)",ret);
    SLE_UT_LOG("[SleAnnounceHdiTest][PASSED]SleHdiStopAnnounceTestLevel0");
}

void SleHdiSetAnnounceDataTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    uint8_t *g_announceData = "announceDataTest";
    uint8_t *g_responceData = "responseDataTest";
    SleAnnounceData g_data = {
        .announceLength = (uint16_t)strlen(g_announceData),
        .announceData = g_announceData,
        .responceLength = (uint16_t)strlen(g_responceData),
        .responceData = g_responceData,
    };
    ret = g_announceHdiUtOpts->setAnnounceData(g_announceId, &g_data);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleAnnounceHdiTest][FAILED] SleHdiSetAnnounceDataTestLevel0 failed(%08X)",ret);
    SLE_UT_LOG("[SleAnnounceHdiTest][PASSED]SleHdiSetAnnounceDataTestLevel0");
}

static SleAnnounceParam g_param = {
    .handle = 1,
    .mode = SLE_ANNOUNCE_CONNECTABLE_SCANABLE_MODE,
    .role = SLE_ANNOUNCE_T_ROAL_CAN_NEGO,
    .level = SLE_ANNOUNCE_NORMAL_LEVEL,
    .channelMap = SLE_ANNOUNCE_DEFAULT_ALL_CHANEL,
    .annonceIntervalMin = 0xc8,
    .annonceIntervalMax = 0xc8,
    .connectIntervalMin = 0x1E,
    .connectIntervalMax = 0x1E,
    .connectLatency = 0x00,
    .connectTimeout = 0x1F4,
    .ownAddr = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
        .addr = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
    },
};
void SleHdiSetAnnounceParamTestLevel0()
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = g_announceHdiUtOpts->setAnnounceParam(g_announceId, &g_param);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleAnnounceHdiTest][FAILED] SleHdiSetAnnounceParamTestLevel0 failed(%08X)",ret);
    SLE_UT_LOG("[SleAnnounceHdiTest][PASSED]SleHdiSetAnnounceParamTestLevel0");
}

void SleAnnounceHdiTestLevel0()
{
    GetSleAnnounceHdiOptsTestLevel0();
    SleHdiRegisterAnnounceCallbacksTestLevel0();
    SleHdiSetAnnounceDataTestLevel0();
    SleHdiSetAnnounceParamTestLevel0();
    SleHdiStartAnnounceTestLevel0();
    sleep(1);
    SleHdiStopAnnounceTestLevel0();
    sleep(1);
}

void GetSleAnnounceHdiOptsTestLevel1(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    //测试入参为NULL
    ret = GetSleAnnounceHdiOpts(NULL);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleAnnounceHdiTest][FAILED] GetSleAnnounceHdiOptsTestLevel1 failed1(%08X)",ret);
    SLE_UT_LOG("[SleAnnounceHdiTest][PASSED]GetSleAnnounceHdiOptsTestLevel1");

    SleAnnounceHdiTestLevel0();
}

void SleHdiRegisterAnnounceCallbacksTestLevel1(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    //测试入参为NULL
    ret = g_announceHdiUtOpts->registerAnnounceCbks(NULL);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleAnnounceHdiTest][FAILED] SleHdiRegisterAnnounceCallbacksTestLevel1 failed1(%08X)",ret);
    SLE_UT_LOG("[SleAnnounceHdiTest][PASSED]SleHdiRegisterAnnounceCallbacksTestLevel1");
    SleHdiRegisterAnnounceCallbacksTestLevel0();
}

void SleHdiSetAnnounceDataTestLevel1(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    //测试入参为NULL
    ret = g_announceHdiUtOpts->setAnnounceData(g_announceId, NULL);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleAnnounceHdiTest][FAILED] SleHdiSetAnnounceDataTestLevel1 failed1(%08X)",ret);
    SLE_UT_LOG("[SleAnnounceHdiTest][PASSED]SleHdiSetAnnounceDataTestLevel1");
    SleHdiSetAnnounceDataTestLevel0();
}

void SleHdiSetAnnounceParamTestLevel1(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    //测试入参为NULL
    ret = g_announceHdiUtOpts->setAnnounceParam(g_announceId, NULL);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleAnnounceHdiTest][FAILED] SleHdiSetAnnounceParamTestLevel1 failed1(%08X)",ret);
    SLE_UT_LOG("[SleAnnounceHdiTest][PASSED]SleHdiSetAnnounceParamTestLevel1");
    SleHdiSetAnnounceParamTestLevel0();
}

void SleHdiStartAnnounceTestLevel1(void)
{
    SleHdiSetAnnounceParamTestLevel0();
}

void SleHdiStopAnnounceTestLevel1(void)
{
    SleHdiSetAnnounceParamTestLevel0();
}

void SleAnnounceHdiTestLevel1()
{
    GetSleAnnounceHdiOptsTestLevel1();
    SleHdiRegisterAnnounceCallbacksTestLevel1();
    SleHdiSetAnnounceDataTestLevel1();
    SleHdiSetAnnounceParamTestLevel1();
    SleHdiStartAnnounceTestLevel1();
    sleep(1);
    SleHdiStopAnnounceTestLevel1();
    sleep(1);
}

void SleAnnounceHdiTestEntry(void)
{
    SLE_UT_LOG("__SleAnnounceHdiTest start");
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = InitSleAnnounceHdiTestEntry();
    SLE_UT_CHECK_AND_RETURN(ret == SLE_HDI_ERRCODE_SUCC);
    SleAnnounceHdiTestLevel0();
    // SleAnnounceHdiTestLevel1();
    ret = DeinitSleAnnounceHdiTestEntry();
    SLE_UT_CHECK_AND_RETURN(ret == SLE_HDI_ERRCODE_SUCC);
    SLE_UT_LOG("__SleAnnounceHdiTest end");
}